﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DiveManager
{
    public partial class AddSupplier : Form
    {

        protected DiveDatabaseEntities diveDatabase;
        
        public string firma;
        public string kdnr_lieferant;

        public string strasse;
        public string hsnr;
        public string adr_zusatz;
        public int plz;
        public string ort;
        public string land;

        public string telefon1;
        public string telefon2;
        public string mobil1;
        public string mobil2;
        public string fax;
        public string email;
        public string www;

        public string bank;
        public string konto;
        public string blz;
        public string iban;
        public string bic;

        public string ust_id;
        public string steuernummer;
        public string finanzamt;
        public string reg_eintrag;
        public string reg_sitz;


        
        
        public AddSupplier()
        {
            InitializeComponent();

            // Init Database
            diveDatabase = new DiveDatabaseEntities();
        }

        private void AddSupplier_Load(object sender, EventArgs e)
        {

        }

        private void exit_Click(object sender, EventArgs e)
        {
            //Beenden von AddSupplier
            this.Close();
        }

        private void add_Click(object sender, EventArgs e)
        {
            //TextBoxen auslesen und in Variablen speichern
            firma = txSupplier.Text;
            kdnr_lieferant = txKDNR.Text;
            strasse = txStrasse.Text;
            hsnr = txHSNR.Text;
            adr_zusatz = txAdrZusatz.Text;

            if (txPLZ.Text.Equals(""))
            {
                plz = 0;
            }
            else
            {
                plz = Convert.ToInt32(txPLZ.Text);
            }

            ort = txOrt.Text;
            land = txLand.Text;
            telefon1 = txTelefon1.Text;
            telefon2 = txTelefon2.Text;
            mobil1 = txMobil1.Text;
            mobil2 = txMobil2.Text;
            fax = txFax.Text;
            email = txEmail.Text;
            www = txWWW.Text;
            bank = txBank.Text;
            konto = txKonto.Text;
            blz = txBLZ.Text;
            iban = txIBAN.Text;
            bic = txBIC.Text;
            ust_id = txUST.Text;
            steuernummer = txSteuerNr.Text;
            finanzamt = txFinanzamt.Text;
            reg_eintrag = txRegEintrag.Text;
            reg_sitz = txRegSitz.Text;

            //Eingaben überprüfen
            if (txSupplier.Text.Equals(""))
            {
                //Messagebox mit Fehlermeldung
                MessageBox.Show("Bite Firmenname eingeben!");
            }
            else
            {
                if (txKDNR.Text.Equals(""))
                {
                    //Messagebox mit Fehlermeldung
                    MessageBox.Show("Bitte Kundennummer beim Lieferanten eingeben!");
                }
                else
                {
                    //Erstellen der GUID
                    Guid guid = System.Guid.NewGuid();
                    //Schreiben in DB
                    //--------------------------------------------------------------------
                    //In die Tabelle Supplier_Main schreiben
                    // Generate new Object for DB
                    Supplier_Main newsupplier_Main = new Supplier_Main();
                    newsupplier_Main.ID = guid;
                    newsupplier_Main.Firma = firma;
                    newsupplier_Main.KDNR_Lieferant = kdnr_lieferant;
                    // Add to DB
                    diveDatabase.AddToSupplier_Main(newsupplier_Main);
                    diveDatabase.SaveChanges();
                    //--------------------------------------------------------------------
                    //In die Tabelle Supplier_Adress schreiben
                    // Generate new Object for DB
                    Supplier_Adress newsupplier_adress = new Supplier_Adress();
                    newsupplier_adress.ID = guid;
                    newsupplier_adress.Strasse = strasse;
                    newsupplier_adress.HSNR = hsnr;
                    newsupplier_adress.Adr_Zusatz = adr_zusatz;
                    newsupplier_adress.PLZ = plz;
                    newsupplier_adress.Ort = ort;
                    newsupplier_adress.Land = land;
                    //Add to DB
                    diveDatabase.AddToSupplier_Adress(newsupplier_adress);
                    diveDatabase.SaveChanges();
                    //--------------------------------------------------------------------
                    //In die Tabelle Supplier_Kommunication schreiben
                    // Generate new Object for DB
                    Supplier_Kommunication newsupplier_kom = new Supplier_Kommunication();
                    newsupplier_kom.ID = guid;
                    newsupplier_kom.Telefon1 = telefon1;
                    newsupplier_kom.Telefon2 = telefon2;
                    newsupplier_kom.Mobil1 = mobil1;
                    newsupplier_kom.Mobil2 = mobil2;
                    newsupplier_kom.Fax = fax;
                    newsupplier_kom.Email = email;
                    newsupplier_kom.WWW = www;
                    // Add to DB
                    diveDatabase.AddToSupplier_Kommunication(newsupplier_kom);
                    diveDatabase.SaveChanges();
                    //--------------------------------------------------------------------
                    //In die Tabelle Supplier_Financial schreiben
                    // Generate new Object for DB
                    Supplier_Financial newsupplier_fin = new Supplier_Financial();
                    newsupplier_fin.ID = guid;
                    newsupplier_fin.Bank = bank;
                    newsupplier_fin.Konto = konto;
                    newsupplier_fin.BLZ = blz;
                    newsupplier_fin.IBAN = iban;
                    newsupplier_fin.BIC = bic;
                    // Add to DB
                    diveDatabase.AddToSupplier_Financial(newsupplier_fin);
                    diveDatabase.SaveChanges();
                    //--------------------------------------------------------------------
                    //In die Tabelle Supplier_Registration schreiben
                    // Generate new Object for DB
                    Supplier_Registration newsupplier_reg = new Supplier_Registration();
                    newsupplier_reg.ID = guid;
                    newsupplier_reg.UST_ID = ust_id;
                    newsupplier_reg.STNR = steuernummer;
                    newsupplier_reg.Finanzamt = finanzamt;
                    newsupplier_reg.Reg_Eintrag = reg_eintrag;
                    newsupplier_reg.Reg_Sitz = reg_sitz;
                    // Add to DB
                    diveDatabase.AddToSupplier_Registration(newsupplier_reg);
                    diveDatabase.SaveChanges();
                    //-------------------------------------------------------------------
                    //Erstellen einer GUID für den HYstorieneintrag
                    Guid guid_history = System.Guid.NewGuid();
                    //In die Tabelle Supplier_History schreiben
                    // Generate new Object for DB
                    Supplier_History newsupplier_History = new Supplier_History();
                    newsupplier_History.ID = guid_history;
                    newsupplier_History.Supplier_ID = guid;
                    newsupplier_History.Changes = "Neuer Lieferant angelegt";
                    newsupplier_History.User = "Funktioniert nicht";
                    // Add to DB
                    diveDatabase.AddToSupplier_History(newsupplier_History);
                    diveDatabase.SaveChanges();
                    //------------------------------------------------------------
                    //------------------------------------------------------------
                    //Eintrag ins Reporting System
                    //Erstellen der GUID
                    Guid reportguid = System.Guid.NewGuid(); 
                    //In die Tabelle ReportingMain schreiben
                    ReportingMain reporting_main = new ReportingMain();
                    reporting_main.ID = reportguid ;
                    reporting_main.Report_Text = "Neuer Lieferant angelegt";
                    reporting_main.Sortierung = 1000;
                    reporting_main.Objekt = firma;
                    // Add to DB
                    diveDatabase.AddToReportingMain(reporting_main);
                    diveDatabase.SaveChanges();
                    //In die Tabelle ReportingUser schreiben


                    //Auf vollständigkeit der Einträge überprüfen

                    //Eintrag ins DataCheck System
                        //Wenn unvollständig eintrag in Reporting


                    //MessageBox, dass anlegen erfolgreich war
                    MessageBox.Show("Lieferant erfolgreich angelegt!");
                                        
                    //TextBoxen leeren
                    this.txSupplier.Clear();
                    this.txKDNR.Clear();
                    this.txStrasse.Clear();
                    this.txHSNR.Clear();
                    this.txAdrZusatz.Clear();
                    this.txPLZ.Clear();
                    this.txOrt.Clear();
                    this.txLand.Clear();
                    this.txTelefon1.Clear();
                    this.txTelefon2.Clear();
                    this.txMobil1.Clear();
                    this.txMobil2.Clear();
                    this.txFax.Clear();
                    this.txEmail.Clear();
                    this.txWWW.Clear();
                    this.txBank.Clear();
                    this.txKonto.Clear();
                    this.txBLZ.Clear();
                    this.txIBAN.Clear();
                    this.txBIC.Clear();
                    this.txUST.Clear();
                    this.txSteuerNr.Clear();
                    this.txFinanzamt.Clear();
                    this.txRegEintrag.Clear();
                    this.txRegSitz.Clear();
                }
            }

        }
    }
}
